package org.dreamwork.jasmine2.web.html;

import org.dreamwork.jasmine2.engine.HttpContext;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.parser.Tag;
import org.dreamwork.jasmine2.util.StringConverter;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.WebContainer;
import org.dreamwork.util.StringUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-1-30
 * Time: 22:51:45
 */
public class GenericHtmlControl extends WebContainer {
    private String namespace;
    private boolean i18n;

    private static final String[] singleTag = {
            "br", "input", "hr", "img", "image", "link", "meta"
    };

    public GenericHtmlControl () {
        viewStateEnabled = false;
    }

    public GenericHtmlControl (String tagName) {
        htmlTagName = tagName;
        viewStateEnabled = false;
    }

    public GenericHtmlControl (Tag tag) {
        htmlTagName = tag.getName ();
        String[] names = tag.getAttributeNames ();
        for (String name : names) {
            setAttribute (name, tag.getAttribute (name));
        }
    }

    public boolean isI18n () {
        return i18n;
    }

    public void setI18n (boolean i18n) {
        this.i18n = i18n;
    }

    public String getNamespace () {
        return namespace;
    }

    public void setNamespace (String namespace) {
        this.namespace = namespace;
        int pos = htmlTagName.indexOf (':');
        if (pos > 0) htmlTagName = htmlTagName.substring (pos + 1);

        if (!StringUtil.isEmpty (namespace)) {
            htmlTagName = namespace + ':' + htmlTagName;
        }
    }

    @Override
    public void setClientId (String clientId) {
        super.setClientId (clientId);
        setAttribute ("id", clientId);
        if (!attrs.containsKey ("name") && !clientId.startsWith ("_auto_")) {
            setAttribute ("name", clientId);
        }
    }

    @Override
    public void setId (String id) {
        super.setId (id);
        if (StringUtil.isEmpty (clientId)) {
            setClientId (id);
        }
    }

    @Override
    public void setName (String name) {
        super.setName (name);
        setAttribute ("name", name);
    }

    @Override
    public void setAttribute (String attrName, String attrValue) {
        if ("clientId".equalsIgnoreCase (attrName))
            setClientId (attrValue);
        else if ("visible".equalsIgnoreCase (attrName))
            setVisible (Boolean.valueOf (attrValue));
        super.setAttribute (attrName, attrValue);
    }

    public IWebControl copy () {
        GenericHtmlControl copy = new GenericHtmlControl (htmlTagName);
        for (Object o : attrs.keySet ()) {
            String key = (String) o;
            copy.attrs.setProperty (key, attrs.getProperty (key));
        }

        for (Iterator<String> i = getStyleNames (); i.hasNext ();) {
            String key = i.next ();
            copy.style.put (key, getStyle (key));
        }

        for (Object o : dynamicAttributes.keySet ()) {
            String key = (String) o;
            copy.dynamicAttributes.put (key, dynamicAttributes.get (key));
        }
        copy.page = page;
        if (name != null)
            copy.setAttribute ("name", name);
        return copy;
    }

    @Override
    public void onPrerender (IWebControl sender, HttpContext context) throws EventException {
/*
        if ("html".equalsIgnoreCase (htmlTagName)) {
            if (getAttribute ("xmlns") == null)
                setAttribute ("xmlns", "http://www.w3.org/1999/xhtml");
        }

*/
        if ((isCheckBox () || isRadioButton ())) {
            String checked = getAttribute ("checked");
            if (!StringUtil.isEmpty (checked)) {
                Boolean b = (Boolean) StringConverter.convertFromString (Boolean.class, checked);
                if (b != null && !b)
                    attrs.remove ("checked");
            }
        }

        String clientId = getClientId ();
        if (clientId != null && clientId.startsWith ("_auto_")) {
            attrs.remove ("clientId");
            attrs.remove ("id");
        }

        super.onPrerender (sender, context);
    }

    protected void beginTag (PrintWriter writer) throws IOException {
        if (isSingleTag (htmlTagName)) {
            writer.write ("<" + htmlTagName);
            renderAttributes (writer);
            writer.write ("/>");
        } else
            super.beginTag (writer);
    }

    protected void endTag (PrintWriter writer) {
        if (!isSingleTag (htmlTagName))
            super.endTag (writer);
    }

    private boolean isSingleTag (String tagName) {
        for (String s : singleTag) {
            if (s.equalsIgnoreCase (tagName)) return true;
        }
        return false;
    }

    private boolean isCheckBox () {
        return "input".equalsIgnoreCase (htmlTagName) && "checkbox".equalsIgnoreCase (getAttribute ("type"));
    }

    private boolean isRadioButton () {
        return "input".equalsIgnoreCase (htmlTagName) && "radio".equalsIgnoreCase (getAttribute ("type"));
    }
}